package com.dlmz.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class DynamicManage extends JFrame implements ActionListener{
	public static void main(String[] args)   
    {    
        new DynamicManage();  
    } 

    JLabel yearRangeLab = new JLabel("年度范围:");  
    JTextField yearStartTxt = new JTextField(4);  
    JLabel connectingLab = new JLabel("~"); 
    JTextField yearEndTxt = new JTextField(4);  
    JButton queryBtn = new JButton("查询");  
    JButton statiscBtn = new JButton("年平均增长统计");  
    private JScrollPane jsp;
	private JTable resultTb; 
	private HealthChangeModel hm;
	String sql = "SELECT * from st_health_person_view";  
	public DynamicManage()  
    {  
        super("动态统计");
        /***************************初始化面板控件***********************/  
      //========查询栏  
        
        JPanel top = new JPanel();  

        top.add(yearRangeLab);  
        top.add(yearStartTxt);  
        top.add(connectingLab);  
        top.add(yearEndTxt);  
        top.add(queryBtn);  
        top.add(statiscBtn);  
        
        //......添加查询栏监听  
        queryBtn.addActionListener((ActionListener) this);  
        queryBtn.setActionCommand("query");  //确定使用的组件
        //......添加年平均增长统计监听 
        statiscBtn.addActionListener((ActionListener) this);  
        statiscBtn.setActionCommand("statisc");  //确定使用的组件
        
        resultTb = new JTable(null);  
        jsp = new JScrollPane(resultTb);  
        
        this.add(top,BorderLayout.NORTH); 
        //......构建整体布局    
        this.add(jsp,BorderLayout.CENTER);  
        //========设置窗口属性  
        validate();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(1000,400);
		this.setLocationRelativeTo(null);
		setVisible(true);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				if (JOptionPane.showConfirmDialog(DynamicManage.this, "是否退出统计？")==JOptionPane.OK_OPTION) {
					dispose();
					new StatisJframe();
				}else {
					new DynamicManage();
				}
			}
		});
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getActionCommand().equals("query")) {  
            /*********************查询***********************/  
            //========获取输入开始结束年分
            String yearStart= yearStartTxt.getText().trim();
            String yearEnd= yearEndTxt.getText().trim();

            if(yearStart.length() != 0 && yearEnd.length()  != 0) {  
                //========开始结束年分输入有效时，执行查询  
                //......定义参数  
            	String newSql = "SELECT * from st_health_person_view where exmYear=? "; 
                //......更新模型  
                jtableUpdate(newSql, yearStart, yearEnd);  
            } else {  
            	//========姓名为空时，设置提醒  
                JOptionPane.showMessageDialog(this, "未输入年度范围");  
            }  
        } else if(e.getActionCommand().equals("statisc")) {  
        	// 统计平均年增长率
        	hm = new HealthChangeModel();  
        	// 保留3位小数
        	String avgRate = String.format("%.3f", hm.queryAvgHealthIncr(sql)) + "%";
        	JOptionPane.showMessageDialog(this, "平均年增长率："+avgRate);  
        	
        }
	}
	
	 //========更新JTable内数据  
    public void jtableUpdate(String sql, String startYear, String endYear)  
    {  
        //......创建模型  
        hm = new HealthChangeModel();  
        hm.queryHealthIncrease(sql,startYear,endYear);  
        //......更新显示  
        resultTb.setModel(hm);  
    } 
}
